package com.xuzz.xrpc.spring.context.xml.schema;
import com.xuzz.xrpc.common.ConfigManager;
import com.xuzz.xrpc.common.PropertyConfig;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
 * @description:
 * @author: xuzz
 * @create: 2020-02-22 18:50
 */
public class PropertyConfigElementUtil {
    public static void parsePropertyConfigFromElement(Element element)
    {
        PropertyConfig propertyConfig = ConfigManager.propertyConfig;
        String nodeLocalName = element.getLocalName();
        if("server".equals(nodeLocalName))
        {
            String serverName = element.getAttribute("name");
            if(!StringUtils.isEmpty(serverName))
            {
                propertyConfig.setServerName(serverName);
            }

            //server address -> ip:port
            String serverAddress = element.getAttribute("address");
            if(!StringUtils.isEmpty(serverAddress))
            {
                propertyConfig.setServerAddress(serverAddress);
            }
            else
            {
                propertyConfig.setServerAddress("127.0.0.1:12888");
            }
        }
        if("registry".equals(nodeLocalName))
        {
            String address = element.getAttribute("address");
                String protocol = address.split("//")[0].replace(":","");

                if(!StringUtils.isEmpty(protocol))
                {
                    propertyConfig.setRegistryProtocol(protocol);
                }
                else
                {
                    propertyConfig.setRegistryProtocol("zookeeper");
                }

            propertyConfig.setRegistryAddress(address.split("//")[1]);
        }
        if("cluster".equals(nodeLocalName))
        {
            String loadBalance =element.getAttribute("loadbalance");
            if(!StringUtils.isEmpty(loadBalance))
            {
                propertyConfig.setLoadBalance(loadBalance);
            }
            else
            {
                propertyConfig.setLoadBalance("random");
            }
        }
    }
}
